package com.rongshu.view.discovery.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.discovery.adapter.SerialListAdapter;
import com.rongshu.view.discovery.bean.SerialListResponse;
import com.rongshu.view.discovery.presenter.SerialPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * created by zhengweis on 2019/3/2.
 * description：连载列表
 */
public class SerialActivity extends BaseActivity<SerialPresenter> implements INetView {
    @BindView(R.id.head_left)
    RelativeLayout headLeft;
    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.serial_list)
    RecyclerView serialList;
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshLayout;
    private SerialListAdapter adapter;
    private List<SerialListResponse.ResponseBean> list;
    private int pageIndex = 1;
    private int pageSize = 10;

    @Override
    protected int initLayout() {
        return R.layout.activity_serial;
    }

    @Override
    protected void initView() {
        headMiddle.setText("连载");
        headLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        list = new ArrayList<>();
        adapter = new SerialListAdapter(this, list);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        serialList.setLayoutManager(manager);
        serialList.setAdapter(adapter);
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.resetNoMoreData();
                pageIndex = 1;
                presenter.getSerialList(pageIndex, pageSize);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getSerialList(pageIndex, pageSize);
            }
        });
    }

    @Override
    protected void initData() {
        setPresenter(new SerialPresenter(this), this);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    public void success(Object object, int tag) {
        SerialListResponse response = (SerialListResponse) object;
        if (response == null) {
            return;
        }
        if (pageIndex == 1) {
            list.clear();
        }
        if (refreshLayout.isLoading()) {
            refreshLayout.finishLoadmore();
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh();
        }
        List<SerialListResponse.ResponseBean> itemBeanList = response.getResponse();
        list.addAll(itemBeanList);
        adapter.notifyDataSetChanged();
        if (itemBeanList.size() == 0) {
            refreshLayout.finishLoadmoreWithNoMoreData();
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }
}
